<?php

class ActiveController extends BaseController
{

    public function init()
    {
        $this->user_id;
        /* Initialize action controller here */
        $this->req = $this->getRequest();
    }

    public function perpayAction()
    {
        // 检查请求
        if (!$this->req->getPost()) {
            return false;
        }
        $order_data = array();
        $order_data['contact'] = $this->req->getPost('contact');
        $order_data['tel'] = $this->req->getPost('tel');
        $order_data['adds'] = $this->req->getPost('adds');
        $order_data['remark'] = $this->req->getPost('remark');
        $order_data['order_id'] = $this->req->getPost('order_id');
        $order_data['appointment_time'] = $this->req->getPost('appointment_time');
        $order_data['actionType'] = 'submit';
        $order_data['platform'] = 'WX';
        $order_data['payment'] = '1';
        /*请求submit接口，生成预订单*/
        $article = new \Base\Rest();
        $order_payload = array();
        $order_payload['payload'] = base64_encode(http_build_query($order_data));
        $req = array('uid' => $this->user_id);
        $rest_res = $article->put('Order', $order_data['order_id'], $req, $order_payload);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            return Response::Error(0, $this->object2array($res));
        }
        return Response::Error(-1, '订单错误!-prepay');
    }

    /*发起支付*/
    public function successAction()
    {
        // 检查请求
        if (!$this->req->isGet()) {
            return false;
        }
        $order_id = $this->req->get('order_id');
        if (empty($order_id)) {
            return Response::Error(-1, 'order null!');
        }
//        $open_id = $this ->getOpenId();
        $open_id = $this->open_id;
        $payload = array();
        $prepay['actionType'] = 'prePay';
        $prepay['platform'] = 'WX';
        $prepay['open_id'] = $open_id;
        $payload['payload'] = base64_encode(http_build_query($prepay));
        $req = array('uid' => $this->user_id);
        /*请求perpay接口，请求微信支付*/
        $article = new \Base\Rest();
        $rest_res = $article->put('Order', $order_id, $req, $payload);
        if ($rest_res->isSuccess()) {
            $res = $rest_res->getResponse();
            $res = $this->object2array($res);
            $res ['data']['rsa']['paySign'] = $res['data']['rsa']['sign'];
            unset($res['CallInfo']['rsa']['sign']);
            $this->view->info = $res ['data']['rsa'];
        }
    }

    public function commentAction()
    {

    }

    private function object2array(&$object)
    {
        $object = json_decode(json_encode($object), true);
        return $object;
    }
}